package com.hndfsj.common.service.impl;

import java.util.Date;

import org.springframework.stereotype.Service;

import com.hndfsj.common.service.ISynchroService;
import com.hndfsj.framework.utils.DateUtils;
import com.hndfsj.framework.utils.RedisUtil;

@Service("synchroService")
public class SynchroServiceImpl implements ISynchroService {

	private RedisUtil redisUtil;

	@Override
	public void saveUser(String userName) {
		save("user", userName);
	}

	@Override
	public String getSynchroUser(String userName) {
		return getSynchro("user", userName);
	}

	@Override
	public void saveDept(String userName) {
		save("dept", userName);
	}

	@Override
	public String getSynchroDept(String userName) {
		return getSynchro("dept", userName);
	}

	@SuppressWarnings("static-access")
	private String getSynchro(String type, String userName) {
		String time = "";// 同步时间
		if (redisUtil.getJedis().get(("synchro-" + type + "-" + userName)) == null) {
			time = DateUtils.formatDate(new Date(), DateUtils.DATETIME_SECOND_FORMAT);
			redisUtil.getJedis().set("synchro-" + type + "-" + userName, time);
		} else {
			time = redisUtil.getJedis().get("synchro-" + type + "-" + userName);
			redisUtil.getJedis().set(("synchro-" + type + "-" + userName).getBytes(),
					DateUtils.formatDate(new Date(), DateUtils.DATETIME_SECOND_FORMAT).getBytes());
		}
		return time;
	}

	@SuppressWarnings("static-access")
	private void save(String type, String userName) {
		redisUtil.getJedis().set(("synchro-" + type + "-" + userName),
				DateUtils.formatDate(new Date(), DateUtils.DATETIME_SECOND_FORMAT));
	}
}
